var locationData = require('../../../staticData/locationKeyValue.json');

exports.customerList = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var searchType = restfulParams.searchType;

    var u_id = restfulParams.u_id;

    var condition = '';

    var is_manager = restfulParams.is_manager;//是否是管理 0-否 1-是

    var u_type = restfulParams.u_type;

    var u_osid = restfulParams.os_id;

    var c_uid_condition = '';

    var salesArr = [];


    console.log("11111111111")
    var userInStr = "";

    //查询员工
    function salesSearch(cb) {
        if (is_manager == 1 && (u_type == 1 || u_type == 2)) {

            //战略  查询战略伙伴下所有销售
            console.log("战略 || 代理");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_master_osid/" + u_osid,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 1 && u_type == 3) {
            //销售主管  查询该销售主管下所有销售.包括自己。
            console.log("销售组长");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_leader/" + restfulParams.u_id,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    salesArr = data.result.data;

                    cb();

                }
            )
        } else if (is_manager == 0 && u_type == 3) {
            //普通销售
            console.log("普通销售");
            salesArr.push({
                u_id: u_id
            });
            cb();
        } else if (u_type == 0) {
            //超管
            console.log("超管");
            R.SEND_HTTP(
                req,
                res, {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url,
                    method: 'POST',
                    data: {
                        fields: ['u_id'].join(",")
                    }
                },
                function (data) {

                    console.log("22222222")
                    console.log(data)

                    salesArr = data.result.data;
                    cb();

                }
            )
        }
    }

    // 拼接in语句查询条件
    function fixInCondition(cb) {
        for (var i = 0; i < salesArr.length; i++) {
            userInStr += salesArr[i].u_id + ","
        }
        userInStr = userInStr.substring(0, userInStr.length - 1);
        if (userInStr == '') {
            userInStr = "null";
        }
        if (u_type > 0) {

            if(searchType != 4){
                c_uid_condition = "/in/c_uid-" + userInStr;
            }
            
        }

        cb()
    }

    //查询客户信息
    function customerInfo(cb) {
        console.log("3333333")
        //0-全部 1-跟进中 2-已成交 3-待处理
        if (searchType == 1) {

            condition = "/c_follow_status/1";

        } else if (searchType == 2) {

            condition = "/c_is_buy/1";

        } else if (searchType == 3) {

            condition = "/c_service_status/0/c_follow_status/0";

        } else if(searchType == 4){
            condition = "/c_follow_status/2";
        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_is_delete/0/" + c_uid_condition + condition + "/sort_order/c_create_time-desc",
                method: 'POST',
                data: {
                    fields: ['c_id', 'c_name', 'c_create_time', 'c_header_url', 'c_phone', 'c_campus_name', 'c_provinces', 'c_city', 'c_area', 'c_address', 'c_distribution_status', 'c_follow_status', 'c_evaluation', 'c_service_status', 'c_is_buy'].join(",")
                }
            },
            function (data) {

                console.log(444444444)
                console.log(data)

                resultData.customerInfo = data.result.data;

                for (var i = 0; i < data.result.data.length; i++) {
                    var provinceId = data.result.data[i].c_provinces;
                    var cityId = data.result.data[i].c_city;
                    var areaId = data.result.data[i].c_area;
                    resultData.customerInfo[i].provinceName = locationData[provinceId];
                    resultData.customerInfo[i].cityName = locationData[cityId];
                    resultData.customerInfo[i].areaName = locationData[areaId];
                }

                cb();

            }
        )
    }

    function queryForEachFollowRecord(cb) {

        console.log(555555)

        F.ParallelEach(resultData.customerInfo, function (item, index, callback) {

            //查询客户跟进信息
            // function followRecordUser(eachCb) {

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: CORE_APIConfig.coreAPI.vFollowRecordUserSearch.Request_Api_Url + "/count/1/cfr_cid/" + item.c_id + "/sort_order/cfr_id-desc",
                        method: 'POST',
                        data: {
                            fields: ['cfr_way', 'cfr_create_time', 'u_nickname'].join(',')
                        }
                    },
                    function (data) {

                        if(data.result.data.length>0){
                            item.followInfo = data.result.data[0];
                            item.cfr_createtime = data.result.data[0].cfr_create_time;
                        }else{
                            item.followInfo = [];
                        }

                        item.followCount = data.result.count;

                        callback();

                    }
                )
            // }

            // F.Parallel([
            //     followRecordUser,
            // ], () => {
            //     callback();
            // })
        }, cb)
    }

    // function sortData(cb){
    //     function sortCreateTime(a,b){  
    //         return a.cfr_create_time-b.cfr_create_time  
    //     }
    //     console.log(resultData)

    //     resultData.customerInfo = resultData.customerInfo.sort(sortCreateTime);

    //     cb();
    // }   

    F.Serial([
        salesSearch,
        fixInCondition,
        customerInfo,
        queryForEachFollowRecord,
        // sortData
    ], () => {
        RES.successResponse(res, resultData);
    })


}